package com.gscitysfy.cus.drain.modules.duty.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;

import com.gscitysfy.cus.drain.config.BusinessException;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnClasses;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnRota;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnClassesMapper;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnRotaMapper;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnClassesService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-03-29 15:06:16
 */
@Service
public class MoperDutyMnClassesServiceImpl extends ServiceImpl<MoperDutyMnClassesMapper, MoperDutyMnClasses> implements IMoperDutyMnClassesService {
    @Autowired
    MoperDutyMnClassesMapper moperDutyMnClassesMapper;
    @Autowired
    MoperDutyMnRotaMapper moperDutyMnRotaMapper;
    //值班长类型代码
    private static final String LEAD_CLASS_TYPE = "dutyType2";

    /**
     * 新增班次
     * @param moperDutyMnClassess
     * @return
     */
    @Override
    public String addMoperDutyMnClasses(MoperDutyMnClasses moperDutyMnClassess) {
        //同时只能存在一班启用的值班长班次
        if (StringUtils.equals(LEAD_CLASS_TYPE,moperDutyMnClassess.getCoamClassessType()) && moperDutyMnClassess.getCoamClassessStatu() == DicConstant.ENABLE.longValue()){
            Long classCount = getEnableLeaderClass();
            if (classCount > 0L ){
                throw new BusinessException("已存在启用的值班长班次");
            }
        }
        moperDutyMnClassesMapper.insert(moperDutyMnClassess);
        return moperDutyMnClassess.getCoamKid();
    }

    /**
     * 更新班次
     * @param moperDutyMnClassess
     * @return
     */
    @Override
    public int updateMoperDutyMnClasses(MoperDutyMnClasses moperDutyMnClassess) {
        //同时只能存在一班启用的值班长班次
        if (StringUtils.equals(LEAD_CLASS_TYPE,moperDutyMnClassess.getCoamClassessType()) && moperDutyMnClassess.getCoamClassessStatu() == DicConstant.ENABLE.longValue()){
            long classCount = count(
                    Wrappers.<MoperDutyMnClasses>lambdaQuery()
                            .eq(MoperDutyMnClasses::getCoamClassessStatu, DicConstant.ENABLE)
                            .eq(MoperDutyMnClasses::getCoamUsable, DicConstant.UN_DELETE)
                            .eq(MoperDutyMnClasses::getCoamClassessType, LEAD_CLASS_TYPE)
                            .ne(MoperDutyMnClasses::getCoamKid, moperDutyMnClassess.getCoamKid())
            );
            if (classCount > 0L ){
                throw new BusinessException("已存在启用的值班长班次");
            }
        }
        return moperDutyMnClassesMapper.updateById(moperDutyMnClassess);
    }

    /**
     * 删除值班班次
     * @param ids 班次ID集合
     * @return true or false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMoperDutyMnClasses(String ids) {
        String[] split = ids.split(",");
        //判断班次是否已关联排班，如有关联则所有班次不执行删除
        for (String id:split) {
            Wrapper<MoperDutyMnRota> rotaWrapper = new LambdaQueryWrapper<MoperDutyMnRota>()
                    .eq(MoperDutyMnRota::getCoamDutyPersonId, SecurityUtils.getPrincipal().getUserId())
                    .eq(MoperDutyMnRota::getCoamClassesId, id);
            List<MoperDutyMnRota> moperDutyMnRotas = moperDutyMnRotaMapper.selectList(rotaWrapper);
            if (CollectionUtils.isNotEmpty(moperDutyMnRotas)) {
                return false;
            }
        }
        //批量更新开启事务，发生异常则全部回滚
        for (String id:split) {
            MoperDutyMnClasses dutyClass = new MoperDutyMnClasses();
            dutyClass.setCoamKid(id);
            dutyClass.setCoamUsable(1L);
            moperDutyMnClassesMapper.updateById(dutyClass);
        }
        return true;
    }

    @Override
    public MoperDutyMnClasses detailMoperDutyMnClasses(String id) {
        Wrapper<MoperDutyMnRota> portalUserApplicationsWrapper = new LambdaQueryWrapper<MoperDutyMnRota>()
                .eq(MoperDutyMnRota::getCoamDutyPersonId, SecurityUtils.getPrincipal().getUserId())
                .eq(MoperDutyMnRota::getCoamClassesId, id);
        List<MoperDutyMnRota> moperDutyMnRotas = moperDutyMnRotaMapper.selectList(portalUserApplicationsWrapper);
        MoperDutyMnClasses moperDutyMnClasses = moperDutyMnClassesMapper.selectById(id);
        if (CollectionUtils.isNotEmpty(moperDutyMnRotas)) {
            moperDutyMnClasses.setFlag(false);
        } else {
            moperDutyMnClasses.setFlag(true);
        }
        return moperDutyMnClasses;
    }

    @Override
    public IPage<MoperDutyMnClasses> getAll(Page<MoperDutyMnClasses> page, Integer coamClassessStatu, String coamDutyType, String coamClassessCode) {
        return moperDutyMnClassesMapper.getAll(page, coamClassessStatu, coamDutyType, coamClassessCode);
    }

    @Override
    public List<MoperDutyMnClasses> selectByEnableClassesStatu() {
        return list(
                Wrappers.<MoperDutyMnClasses>lambdaQuery()
                        .eq(MoperDutyMnClasses::getCoamClassessStatu, DicConstant.ENABLE)
                        .eq(MoperDutyMnClasses::getCoamUsable, DicConstant.UN_DELETE)

        );

    }

    /**
     * 获取正在启用的值班长班次数目
     * @return
     */
    @Override
    public Long getEnableLeaderClass(){
        return count(
                Wrappers.<MoperDutyMnClasses>lambdaQuery()
                        .eq(MoperDutyMnClasses::getCoamClassessStatu, DicConstant.ENABLE)
                        .eq(MoperDutyMnClasses::getCoamUsable, DicConstant.UN_DELETE)
                        .eq(MoperDutyMnClasses::getCoamClassessType, LEAD_CLASS_TYPE)

        );
    }
}
